# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
  'require.resolve(
    "react-native/scripts/react_native_pods.rb",
    {paths: [process.argv[1]]},
  )', __dir__]).strip

platform :ios, '15.1'
prepare_react_native_project!

$RNMapboxMapsImpl = 'mapbox'
if ENV['RNMBX10']
  $RNMapboxMapsVersion = '10.19.0'
end

if ENV['CI_MAP_IMPL']
  fail "Invalid CI_MAP_IMPL value" unless ["mapbox","mapbox11"].include?(ENV['CI_MAP_IMPL'])
  case ENV['CI_MAP_IMPL']
  when 'mapbox'
    $RNMapboxMapsImpl = 'mapbox'
    $RNMapboxMapsVersion = '10.19.0'
  when 'mapbox11'
    $RNMapboxMapsImpl = 'mapbox'
    # Using default v11 version from main library
  else
    fail "Invalid CI_MAP_IMPL value: #{ENV['CI_MAP_IMPL']}"
  end
end

linkage = ENV['USE_FRAMEWORKS']
if linkage != nil
  Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
  use_frameworks! :linkage => linkage.to_sym
end

flags = ReactNativePodsUtils.get_default_flags()
if flags[:fabric_enabled]
  pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec', :modular_headers => false
end

production = ENV["PRODUCTION"] == "1"

# We ignore warning except for RNMapboxMaps
INHIBIT_WARNING_BY_DEFAULT = true

if INHIBIT_WARNING_BY_DEFAULT
  ORIG_POD = method(:pod)

  # Override pods so we default to disabling all warnings
  def pod(name, *requirements)
    options = requirements.last
    if options.is_a?(Hash)
      options[:inhibit_warnings] = true unless options.has_key?(:inhibit_warnings)
    else
      requirements.push({inhibit_warnings: true})
    end
    ORIG_POD[name, *requirements]
  end
end

target 'RNMapboxExample' do
  #pod 'rnmapbox-maps', :path => '../../', :inhibit_warnings => false

  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/.."
  )

  pre_install do |installer|
    $RNMapboxMaps.pre_install(installer)
  end

  def disable_optimizations_in_release(installer)
    installer.pods_project.build_configurations.each do |config|
      if config.name.include?("Release")
        config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
        config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone'
        config.build_settings['SWIFT_COMPILATION_MODE'] = 'singlefile'
      end
    end
  end

  post_install do |installer|
    # See https://github.com/rnmapbox/maps/pull/2878
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings["ONLY_ACTIVE_ARCH"] = "NO"
      end
    end
    react_native_post_install(
      installer,
      # Set `mac_catalyst_enabled` to `true` in order to apply patches
      # necessary for Mac Catalyst builds
      :mac_catalyst_enabled => false,
      # :ccache_enabled => true
    )
    $RNMapboxMaps.post_install(installer)
  end
end
